
var userMan = require(".././core/user_manager");
var dbMan = require(".././core/database_manager");
var User = require(".././models/User");
var url = require('url');

exports.login = function(req, res) {
    var email = req.body.email;
    var password = req.body.password;

    try {
        var db = dbMan.connectMySQL();
        userMan.setDatabase(db);

        userMan.authenticate(email, password, function(data) {
            if (data instanceof Error) {
                //  TODO: handle error here, do not just respond with the error,
                //  the code below is just temporary
                req.session.user = undefined;
                console.log(data.message);
                res.send(data.message);
            } else {
                //  user is authenticated, register the user's id in the session
                req.session.user = data;
                console.log('User with id ' + data.id + ' is logged in.');
                res.send("Login Success");
            }
        });
    } finally {
        dbMan.disconnectMySQL();
    }
};

exports.byCompany = function(req, res) {
    var companyName = req.params.companyname;

    try {
        var db = dbMan.connectMySQL();

        userMan.setDatabase(db);

        userMan.getUsersByCompany(companyName, function(data) {
            if (data instanceof Error) {
                //  TODO: handle error here, do not just respond with the error,
                //  the code below is just temporary
                res.send(data.message);
            } else {
                //  valid response
                res.send(data);
            }
        });
    } finally {
        dbMan.disconnectMySQL();
    }
};

exports.availableInCompany = function(req, res) {
    var companyName = req.params.companyname;
    
    try {
        var db = dbMan.connectMySQL();
        
        userMan.setDatabase(db);
        userMan.getAvailableUsersByCompany(companyName, function(data) {
            if (data instanceof Error) {
                //  TODO: handle error here, do not just respond with the error,
                //  the code below is just temporary
                res.send(data.message);
            } else {
                //  valid response
                res.send(data);
            }
        });
    } finally {
        dbMan.disconnectMySQL();
    }
};

exports.all = function(req, res) {
    try {
        var db = dbMan.connectMySQL();
        
        userMan.setDatabase(db);
        userMan.getUsers(function(data) {
            if (data instanceof Error) {
                //  TODO: handle error here, do not just respond with the error,
                //  the code below is just temporary
                res.send(data.message);
            } else {
                //  valid response
                res.send(data);
            }
        });
    } finally {
        dbMan.disconnectMySQL();
    }
};

exports.register = function (req, res) {
    var user = new User();
    
    user.firstName = req.body.firstname;
    user.lastName = req.body.lastname;
    user.companyId = req.body.companyid;
    user.email = req.body.email;
    user.password = req.body.password;
    user.role = req.body.role;
    
    try {
        var db = dbMan.connectMySQL();
        
        userMan.setDatabase(db);
        userMan.register(user, function(data) {
            if (data instanceof Error) {
                //  TODO: handle error here, do not just respond with the error,
                //  the code below is just temporary
                res.send(data.message);
            } else {
                //  valid response
                res.send(data);
            }
        });
    } finally {
        dbMan.disconnectMySQL();
    }
};

exports.makeAvailable = function (req, res) {
    var userId = req.session.user.id;
    
    try {
        var db = dbMan.connectMySQL();
        
        userMan.setDatabase(db);
        userMan.changeAvailability(userId, 'online', function(data) {
            if (data instanceof Error) {
                //  TODO: handle error here, do not just respond with the error,
                //  the code below is just temporary
                res.send(data.message);
            } else {
                //  valid response
                res.send(data);
            }
        });
    } finally {
        dbMan.disconnectMySQL();
    }
};

exports.makeUnavailable = function (req, res) {
    var userId = req.session.user.id;
    
    try {
        var db = dbMan.connectMySQL();
        
        userMan.setDatabase(db);
        userMan.changeAvailability(userId, 'offline', function(data) {
            if (data instanceof Error) {
                //  TODO: handle error here, do not just respond with the error,
                //  the code below is just temporary
                res.send(data.message);
            } else {
                //  valid response
                res.send(data);
            }
        });
    } finally {
        dbMan.disconnectMySQL();
    }
};